	/**
 *    Copyright (C) 2009 Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef _CUBEFS_CORE_API_METADATA_ID_HPP
#define _CUBEFS_CORE_API_METADATA_ID_HPP

#include <cubefs/api/metadata/id.hpp>
#include <cubefs/core/api/metadata/field.hpp>

#define CUBEFS_SOURCE "cubefs/core/api/metadata/id.hpp:" CUBEFS_AT

namespace cubefs { namespace api {

template <typename Interface>
class IDFieldImplT: public FieldMetadataImplT<Interface> {
	typedef IDFieldImplT<Interface> 		Me;
	typedef FieldMetadataImplT<Interface> 	Base;
public:

	IDFieldImplT(Int ptr, Int abi_ptr, const string &name, Int size, Int offset, Int count):
		Base(ptr, abi_ptr, FieldMetadata::ID, name, offset, offset + count*size, size) {}

	IDFieldImplT(Int ptr, Int abi_ptr, const string &name, Int size, Int count):
		Base(ptr, abi_ptr, FieldMetadata::ID, name, 0, count*size, size) {}

	IDFieldImplT(Int ptr, Int abi_ptr, const string &name, Int size):
		Base(ptr, abi_ptr, FieldMetadata::ID, name, 0, size, size) {}


    virtual void GetValue(Page *page, Int idx, TypedIDValue* value, bool abi);
    virtual void SetValue(Page* page, Int idx, TypedIDValue* value, bool abi);

    virtual const IDValue GetIDValue(const void *mem) = 0;
    virtual void SetIDData(const IDValue& id, void* mem) = 0;

	static void Init() {
	    AbstractType* type = new AbstractType(TypeNameFactory<Interface>::name(), FieldMetadata::MyType());
	    type_ = type;

	    type->AddOperation(new BasicOperation<void (Me::*)(Page*, Int, TypedIDValue*, bool)>(type, "GetValue", "page", "idx", "id", "api", &Me::GetValue));
	    type->AddOperation(new BasicOperation<void (Me::*)(Page*, Int, TypedIDValue*, bool)>(type, "SetValue", "page", "idx", "id", "api", &Me::SetValue));
	}

    CUBEFS_DECLARE_REFLECTION;
};


template <typename Interface>
Type* IDFieldImplT<Interface>::type_ 					= NULL;
typedef IDFieldImplT<IDField> 							IDFieldImpl;



template <typename Interface>
void IDFieldImplT<Interface>::GetValue(Page *page, Int idx, TypedIDValue* value, bool abi) {
    if (value == NULL) {
        throw NullPointerException(CUBEFS_SOURCE, "id value must not be NULL");
    }

    if (page == NULL) {
        throw NullPointerException(CUBEFS_SOURCE, "page must not be NULL");
    }

    void* ptr0 = abi ? Base::ValuePtrAbi(page->Ptr(), idx) : Base::ValuePtr(page->Ptr(), idx);

    value->SetValue(this->GetIDValue(ptr0));
}

template <typename Interface>
void IDFieldImplT<Interface>::SetValue(Page* page, Int idx, TypedIDValue* value, bool abi) {
    if (value == NULL) {
        throw NullPointerException(CUBEFS_SOURCE, "id value must not be NULL");
    }

    if (page == NULL) {
        throw NullPointerException(CUBEFS_SOURCE, "page must not be NULL");
    }

    void* ptr0 = abi ? Base::ValuePtrAbi(page->Ptr(), idx) : Base::ValuePtr(page->Ptr(), idx);

    this->SetIDData(value->GetValue(), ptr0);
}

}}

#undef CUBEFS_SOURCE
#endif
